// ==UserScript==
// @name GuanXi Fou plugin
// @author RainX (jingxu@alibaba-inc.com / rainx1982@yahoo.com.cn)
// @namespace http://code.google.com/p/guanxifou/
// @description An plugin for the integration of Yahoo!GuanXi (http://my.cn.yahoo.com) and Fanfou.(Modified From Doufan plugin) 
// @include http://my.cn.yahoo.com/apps/share/*
// ==/UserScript==

// Check if the environment is OK
if (ChkEnv())
{
    // We want to know on which page we are staying and do different things.
    var pageUrl = document.location.href;
	if ((pageUrl.indexOf("type=video") != -1) || 
        (pageUrl.indexOf("type=music") != -1) ||
        (pageUrl.indexOf("type=photo") != -1) ||
        (pageUrl.indexOf("type=docs") != -1))
    {
        // 没有实现的部分
    }
    else if (pageUrl.indexOf("type=link") != -1)
    {
        // 分享链接
		DoShareLink()
    }
    else if (pageUrl.indexOf("type=note") != -1)
    {
        // 分享消息
        DoShareMessage();
    }
	else if (pageUrl.indexOf("editshare.php") != -1 && pageUrl.indexOf("?") == -1 )
	{
        // 分享消息
        DoShareMessage();
	}
	else
	{
		// Do Nothing
	}
}

// 用来根据classname来获取元素的函数
function getElementsByClassName (clsname, elename)
{
	var eles = document.getElementsByTagName(elename);
	var returns = [];
	for ( var i in eles )
	{
		if (eles[i].className == clsname)
		{
			returns.push(eles[i]);
		}
	}
	return returns;
}

// 分享消息
function DoShareMessage()
{
	var div = getElementsByClassName('yc-setleft', 'div')[0];
	var span = document.createElement('span');
	span.className='button basic';
	span.innerHTML = '<button type="button" id="postfanfou">提交并分享到饭否</button>';
	div.appendChild(span);
	var postfanfou = document.getElementById('postfanfou');
	postfanfou.addEventListener('click', PostShareMessage, false);
}

// 分享链接
function DoShareLink()
{
	var div = getElementsByClassName('yc-setleft', 'div')[0];
	var span = document.createElement('span');
	span.className='button basic';
	span.innerHTML = '<button type="button" id="postfanfou">提交并分享到饭否</button>';
	div.appendChild(span);
	var postfanfou = document.getElementById('postfanfou');
	postfanfou.addEventListener('click', PostShareLink, false);

}

// 发送分享的消息
function PostShareMessage(event)
{
	var content = document.getElementById('share_note');
	content = content.value;
	if ( content == '') 
	{
		alert(' 消息为空哦 ');
		return false;
	}
	
	if ( content.length >= 140)
	{
		alert(' 饭否的消息不能超过140个字符哦 ');
		return false;
	}

	SendRequest(content);	 // 提交分享到饭否
 	var div = getElementsByClassName('button highlight', 'span')[0];
	div.firstChild.click(); // 点击模拟提交
	return true;
}

// 发送分享链接
function PostShareLink(event)
{
	var objs = getElementsByClassName('ys-objtext', 'input');
	var link = objs[0].value;
	var objs = getElementsByClassName('ys-objtext', 'textarea');
	var desc = objs[0].value;


	// 检测内容长度

	var content = link + " " + desc;

	if ( link == '' || desc == '') 
	{
		alert(' 链接或者描述不能为空 ');
		return false;
	}
	
	if ( content.length >= 140)
	{
		alert(' 饭否的消息不能超过140个字符哦 ');
		return false;
	}

	SendRequest(content);	 // 提交分享到饭否
 	var div = getElementsByClassName('button highlight', 'span')[0];
	div.firstChild.click(); // 点击模拟提交
	return true;
}

function SendRequest(msg)
{
    GM_xmlhttpRequest({
                        method: 'POST',
                        url: 'http://api.fanfou.com/statuses/update.xml',
                        headers: {'Content-type': 'application/x-www-form-urlencoded'},
                        data: 'status=' + encodeURIComponent(msg),
                        onload: function(responseDetails) {
                            if (responseDetails.status == 200)
                                alert("分享成功！");
                            else
                            {
                                alert('分享失败！\n调试信息:\nreturned status:' + responseDetails.status +
                                    ',statusText:' + responseDetails.statusText + '\n' +
                                    ',responseHeaders:' + responseDetails.responseHeaders + '\n' +
                                    'responseText:\n' + responseDetails.responseText);
                            }
                        }
                      });
}

function ChkEnv()
{
    // Check xmlhttpRequest Support
    if (!GM_xmlhttpRequest)
    {
        alert("您的Greaskmonkey插件不能支持关系否");
        return false;
    }
    return true;
}
